package com.seal.commons.query.utils;


/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-06-28 21:56<br/>
 * @since JDK 1.8
 */
public class PageSqlSupport {


    private static final String COUNT_PAGE_PRE_SQL = "SELECT count(*) FROM ( ";

    private static final String COUNT_PAGE_SUFFIX_SQL = " ) temp ";

    private static final String MYSQL_PAGE_POST_SQL = " LIMIT [rowStart], [pageSize]";

    private static final String ORACLE_PAGE_PRE_SQL = "SELECT * FROM (SELECT INNER.*, ROWNUM as RECORDNUM FROM (";

    private static final String ORACLE_PAGE_POST_SQL = ") INNER WHERE INNER.ROWNUM <= ([pageIndex]*[pageSize]) ) WRAPPED WHERE WRAPPED.RECORDNUM > (([pageIndex]-1)*[pageSize])";

    public static StringBuffer getQuery(String sql, String  databaseType){
        StringBuffer stringBuffer = new StringBuffer(500);
        if("ORACLE".equalsIgnoreCase(databaseType)){
            stringBuffer.append(ORACLE_PAGE_PRE_SQL);
            stringBuffer.append(sql);
            stringBuffer.append(ORACLE_PAGE_POST_SQL);
            return stringBuffer;
        }
        if("MYSQL".equalsIgnoreCase(databaseType)){
            stringBuffer.append(sql);
            stringBuffer.append(MYSQL_PAGE_POST_SQL);
            return  stringBuffer;
        }
        return new StringBuffer(sql);
    }

    public static StringBuffer getCountQuery(String sql, String databaseType){
        StringBuffer stringBuffer = new StringBuffer(500);
        stringBuffer.append(COUNT_PAGE_PRE_SQL);
        stringBuffer.append(sql);
        stringBuffer.append(COUNT_PAGE_SUFFIX_SQL);
        return stringBuffer;
    }
}